package com.example.sicnuaccountingapp.ui.home

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.sicnuaccountingapp.data.entity.Transaction
import com.example.sicnuaccountingapp.data.entity.TransactionType
import com.example.sicnuaccountingapp.databinding.ItemTransactionBinding
import java.text.SimpleDateFormat
import java.util.*

class TransactionAdapter(
    private val onItemClick: (Transaction) -> Unit
) : ListAdapter<Transaction, TransactionAdapter.TransactionViewHolder>(TransactionDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder {
        val binding = ItemTransactionBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return TransactionViewHolder(binding)
    }

    override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class TransactionViewHolder(
        private val binding: ItemTransactionBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        init {
            binding.root.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    onItemClick(getItem(position))
                }
            }
        }

        fun bind(transaction: Transaction) {
            binding.apply {
                // 设置金额，根据类型显示正负号
                val amountText = when (transaction.type) {
                    TransactionType.EXPENSE -> "-¥%.2f"
                    TransactionType.INCOME -> "+¥%.2f"
                }.format(transaction.amount)
                
                amount.text = amountText
                amount.setTextColor(
                    amount.context.getColor(
                        when (transaction.type) {
                            TransactionType.EXPENSE -> android.R.color.holo_red_dark
                            TransactionType.INCOME -> android.R.color.holo_green_dark
                        }
                    )
                )

                // 设置备注
                note.text = transaction.note

                // 设置时间
                val dateFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
                date.text = dateFormat.format(Date(transaction.date))

                // TODO: 设置分类名称和图标
                categoryName.text = transaction.categoryName
                categoryIcon.setImageResource(android.R.drawable.ic_menu_help)
            }
        }
    }

    private class TransactionDiffCallback : DiffUtil.ItemCallback<Transaction>() {
        override fun areItemsTheSame(oldItem: Transaction, newItem: Transaction): Boolean {
            return oldItem.transactionId == newItem.transactionId
        }

        override fun areContentsTheSame(oldItem: Transaction, newItem: Transaction): Boolean {
            return oldItem == newItem
        }
    }
} 